<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskConfig extends HelpDeskObject {

	var $show_linkback					= '1';
	var $files_enable					= '1';
	var $files_maxsize					= '3000';
	var $files_storagemethod			= '1';
	var $display_name					= '1';
	var $emails_defaultname				= null;
	var $emails_defaultemail			= null;
	var $emails_includedescription		= '0';
	var $emails_descriptionmaxlength	= '-1';
	var $emails_includecomments			= '0';
	var $emails_commentmaxlength		= '1';
	var $require_login					= '1';

	/**
	 * constructor
	 * @return void
	 */
	function __construct() 
	{
		$this->setVariables();
	}

	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() {

		$query = "SELECT t.* FROM #__helpdesk_config as t ";
		
		return $query;
	}
	
	/**
	 * Retrieves the data
	 * @return array Array of objects containing the data from the database
	 */
	function getData() {
		global $database;
		// load the data if it doesn't already exist
		if (empty( $this->_data )) {
			$query = $this->_buildQuery();
			$database->setQuery( $query );
			$this->_data = $database->loadObjectList( );
		}
		
		return $this->_data;
	}

	/**
	 * Set Variables
	 *
	 * @acces	public
	 * @return	object
	 */
	function setVariables() {
		$success = false;
		
		if ( $data = $this->getData() ) {
			for ($i=0; $i<count($data); $i++) {
				$title = $data[$i]->title;
				$value = $data[$i]->value;
				if (!empty($title)) {
					$this->$title = $value;
				}
			}
			
			$success = true;
		}
		
		return $success;
	}	

	/**
	 * Get component config
	 *
	 * @acces	public
	 * @return	object
	 */
	function &getInstance() {
		static $instance;

		if (!is_object($instance)) {
			$instance = new HelpDeskConfig();
		}

		return $instance;
	}

	/**
	 * 
	 * @param $id
	 * @return unknown_type
	 */
	function getUser( $id=null )
	{
		if (!empty($id))
		{
			$database = HelpDeskConfig::getDBO();
			$row = new mosUser( $database );
			$user = $row->load( $id );
			
		} else 
		{
			global $my;
			$user = $my;
		}
		
		return $user;	
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function getApplication( $refresh=false ) 
	{
		static $instance;
		
		if (!is_object($instance) || $refresh) 
		{
			// do this to create J1.5 compatible functions, such as loadObject()
			$instance = HelpDeskApplication::getInstance( $refresh );			
		}

		return $instance;
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function getDBO( $refresh=false ) 
	{
		static $instance;
		
		if (!is_object($instance) || $refresh) 
		{
			// do this to create J1.5 compatible functions, such as loadObject()
			global $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
			$instance = new HelpDeskDatabase( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );			
		}

		return $instance;
	}
	
	/**
	 * Return a reference to the {@link JDate} object
	 *
	 * @access public
	 * @param mixed $time The initial time for the JDate object
	 * @param int $tzOffset The timezone offset.
	 * @return object JDate
	 * @since 1.5
	 */
	function &getDate($time = 'now', $tzOffset = 0)
	{
		static $instances;

		if(!isset($instances)) {
			$instances = array();
		}

		$classname = 'HelpDeskDate';
		$key = $time . '-' . $tzOffset;

		if(!isset($instances[$classname][$key])) {
			$tmp = new $classname($time, $tzOffset);
			//We need to serialize to break the reference
			$instances[$classname][$key] = serialize($tmp);
			unset($tmp);
		}

		$date = unserialize($instances[$classname][$key]);
		return $date;
	}

	/**
	 * 
	 * @return unknown_type
	 */
	function &getFromXML( $needle='version' ) 
	{
		// TODO Needs to be updated
		global $mosConfig_absolute_path;
		require_once( $mosConfig_absolute_path . '/includes/domit/xml_domit_lite_include.php' );
		
		$success = "null";
		$pkg = strtolower( "com_HelpDesk" );
		// $row = new HelpDeskObject();
		
		/* Get the component base directory */
		$adminDir = JPATH_ADMINISTRATOR .DS. 'components';
		$siteDir = JPATH_SITE .DS. 'components';

		/* Get the component folder and list of xml files in folder */
		$folder = $adminDir.DS.$pkg;
		
		if (HelpDeskFolder::exists($folder)) 
		{
			$xmlFilesInDir = HelpDeskFolder::files($folder, '.xml$');
		} else {
			$folder = $siteDir.DS.$pkg;
			if (HelpDeskFolder::exists($folder)) {
				$xmlFilesInDir = HelpDeskFolder::files($folder, '.xml$');
			} else {
				$xmlFilesInDir = null;
			}
		}

		//if there were any xml files found
		if (count($xmlFilesInDir))
		{
			foreach ($xmlFilesInDir as $xmlfile)
			{
				// Read the file to see if it's a valid component XML file
				$xmlDoc = new DOMIT_Lite_Document();
				$xmlDoc->resolveErrors( true );
	
				if (!$xmlDoc->loadXML( $folder . DS . $xmlfile, false, true )) {
					continue;
				}
	
				$root = &$xmlDoc->documentElement;
	
				if ($root->getTagName() != 'mosinstall') {
					continue;
				}
				if ($root->getAttribute( "type" ) != "component") {
					continue;
				}
	
				$element = &$root->getElementsByPath( $needle, 1);
				$success = $element ? $element->getText() : HelpDeskText( "Undefined" );
			}
		}

		return $success;
	}
	
	/**
	 * 
	 * @param $fieldname
	 * @return unknown_type
	 */
	function getFieldname( $fieldname )
	{
		// use combo of option, view, and layout to find specific variable	
		$o = strtolower( "com_HelpDesk" );
		$v = HelpDeskRequest::getVar( 'view', 'default' );
		$l = HelpDeskRequest::getVar( 'layout', 'default' );
		$return = "{$o}_{$v}_{$l}_{$fieldname}";
		return $return;
	}
	
	/**
	 * Method to dump the structure of a variable for debugging purposes
	 *
	 * @param	mixed	A variable
	 * @param	boolean	True to ensure all characters are htmlsafe
	 * @return	string
	 * @since	1.5
	 * @static
	 */
	function dump( &$var, $htmlSafe = true ) {
		$result = print_r( $var, true );
		return '<pre>'.( $htmlSafe ? htmlspecialchars( $result ) : $result).'</pre>';
	}	
}
?>